package com.summit.exam.dbdao;


import com.summit.exam.MysqlConnection;
import com.summit.exam.entity.GradeBean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class GradeDao {
    //修改分数
    public Boolean updateGrade(int score) {
        Connection conn = MysqlConnection.getMysqlConnection().getCon();
        PreparedStatement stm = null;
        String sql = "update grade set score = ? where id = 1;";
        int ok = 0;

        try {
            stm = conn.prepareStatement(sql);

            stm.setInt(1, score);

            ok = stm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                MysqlConnection.CloseConn(stm, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return (ok != 0);
    }

    //查询分数
    public GradeBean selectGrade() {
        Connection conn = MysqlConnection.getMysqlConnection().getCon();
        PreparedStatement stm = null;
        ResultSet resultSet = null;
        GradeBean grade = new GradeBean();

        try {
            stm = conn.prepareStatement("SELECT * FROM grade");

            resultSet = stm.executeQuery();

            while (resultSet.next()){
                grade.setId(resultSet.getInt("id"));
                grade.setScore(resultSet.getInt("score"));
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                MysqlConnection.CloseConn(stm, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return grade;
    }
}
